<template>
    <div class="login-wrap">
        <div class="ms-login">
            <div class="ms-title">小区业主后台管理</div>
            <el-form  :rules="rules" ref="logindata" label-width="0px" class="ms-content">
                <el-form-item prop="accountName">
                    <el-input v-model="logindata.accountName" placeholder="accountName">
                        <el-button slot="prepend" icon="el-icon-lx-people"></el-button>
                    </el-input>
                </el-form-item>
                <el-form-item prop="accountPassword">
                    <el-input
                            type="password"
                            placeholder="accountPassword"
                            v-model="logindata.accountPassword"
                            @keyup.enter.native="submitForm()"
                    >
                        <el-button slot="prepend" icon="el-icon-lx-lock"></el-button>
                    </el-input>
                </el-form-item>

                <router-link to="/Register"><i class="el-icon-user"></i>注册账号</router-link>
                <div class="login-btn">
                    <el-button type="primary" @click="submitForm()">登录</el-button>
                </div>
                <p class="login-tips">Tips : 欢迎来到天上人间</p>
            </el-form>
        </div>
    </div>
</template>

<script>
    export default {
        data: function () {
            var validatePass = (rule, value, callback) => {
                if (value === '') {
                    callback(new Error('请输入密码'));
                } else {
                    callback();
                }
            };
            return {
                logindata: {
                    accountName: 'admin',
                    accountPassword: '123456',
                },
                userId:'',
                rules: {
                    accountName: [{required: true, message: '请输入用户名', trigger: 'blur'}],
                    accountPassword: [{required: true, message: '请输入密码', trigger: 'blur'}],
                },
            };
        },
        methods: {
            submitForm() {
                const _this = this
                console.log(_this.logindata)
                _this.$axios.post("http://localhost:8888/login",_this.logindata).then(res => {

                    console.log(res);
                    //传参格式
                    const _this = this
                    //_this.$router.push("/table")
                    const token = res.headers['authorization']
                    _this.$store.commit('SET_TOKEN', token)//_this是上面定义的，相当于this，获取store的信息，使用commit接口
                    _this.$store.commit('SET_USERINFO', res.data.data)

                    console.log("to dashboard===============================================")
                    _this.$router.push("/dashboard")//跳转
                    console.log("666666")
                }).catch((err) =>{
                    if(err.code===400){
                        alert("无效账户")
                    }
                });
            },
        },
    };
</script>

<style scoped>
    .login-wrap {
        position: relative;
        width: 100%;
        height: 100%;
        background-image: url(../../assets/img/1.jpg);
        background-size: 100%;
    }

    .ms-title {
        width: 100%;
        line-height: 50px;
        text-align: center;
        font-size: 20px;
        color: #fff;
        border-bottom: 1px solid #ddd;
    }

    .ms-login {
        position: absolute;
        left: 50%;
        top: 50%;
        width: 350px;
        margin: -190px 0 0 -175px;
        border-radius: 5px;
        background: rgba(255, 255, 255, 0.3);
        overflow: hidden;
    }

    .ms-content {
        padding: 30px 30px;
    }

    .login-btn {
        text-align: center;
    }

    .login-btn button {
        width: 100%;
        height: 36px;
        margin-bottom: 10px;
    }

    .login-tips {
        font-size: 12px;
        line-height: 30px;
        color: #fff;
    }
</style>
